package com.yxhl.zoume.utils;

import com.yxhl.zoume.common.info.ZMDataInfo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Xboy on 16/4/6.
 */
public class TimeUtils {

    /**
     * 获取当前的年份
     *
     * @return
     */
    public static String getCurrentYear() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        String data = dateFormat.format(new Date());
        return data;
    }

    /**
     * 将日期格式化成字符串
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String data = dateFormat.format(date);
        String[] datas = data.split(" ");
        return datas[0];
    }

    /**
     * 将时间格式化成字符串样式
     *
     * @param time
     * @return
     */
    public static String timeToString(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String data = dateFormat.format(new Date(time));
        return data;
    }

    /**
     * 将时间格式化成字符串样式
     *
     * @param time
     * @return
     */
    public static String exchangeToTime(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = dateFormat.format(new Date(time));
        return data;
    }

    /**
     * “－”分割的格式
     *
     * @param time
     * @return
     */
    public static ZMDataInfo timeToDataInfoLineDiving(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String data = dateFormat.format(new Date(time));
        String[] dataStrs = data.split(" ");
        ZMDataInfo info = new ZMDataInfo();
        info.setData(dataStrs[0])
                .setTime(dataStrs[1]);
        return info;
    }

    /**
     * 年月日 汉子分割的格式
     *
     * @param time
     * @return
     */
    public static ZMDataInfo timeToDataInfoCharacterDiving(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String data = dateFormat.format(new Date(time));
        String[] dataStrs = data.split(" ");
        ZMDataInfo info = new ZMDataInfo();
        info.setData(dataStrs[0])
                .setTime(dataStrs[1]);
        return info;
    }

    /**
     * 判断日期是否是今天
     *
     * @param date
     * @return
     */
    public static boolean isToday(String date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String today = df.format(new Date());
        return today.equals(date);
    }

    /**
     * 前一天的日期
     *
     * @param currentDate
     * @return
     */
    public static String previousDate(String currentDate) {
        return dateWithOffset(currentDate, -1);
    }

    /**
     * 后一天的日期
     *
     * @param currentDate
     * @return
     */
    public static String nextDate(String currentDate) {
        return dateWithOffset(currentDate, 1);
    }

    /**
     * 根据参考日期及其增量获取目标日期
     *
     * @param currentDate
     * @param offset
     * @return
     */
    public static String dateWithOffset(String currentDate, int offset) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String targetDay = "";
        try {
            Date date = dateFormat.parse(currentDate);
            long timeMil = date.getTime() + offset * 24 * 60 * 60 * 1000;

            Date targetData = new Date(timeMil);
            targetDay = dateFormat.format(targetData);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return targetDay;
    }
}
